package com.ebupt.migu.music.reco.service.impl;

import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.reco.mapper.RecoMapper;
import com.ebupt.migu.music.reco.service.RecoService;
import com.ebupt.migu.music.reco.utils.RecoESUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author liuyangyang@ebupt.com
 * @Description
 * @Date 2021/11/10 16:48
 */
@Slf4j
@Service
public class RecoServiceImpl implements RecoService {

    @Autowired
    private RecoESUtils utils;
    @Autowired
    private RecoMapper recoMapper;

    /**
     * 查询场景类型
     *
     * @return
     */
    @Override
    public ResultObject queryScenes() {
        ResultObject res = new ResultObject();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(recoMapper.queryScenes());
            return res;
        } catch (Exception e) {
            log.error("查询场景类型出错：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 创建出库规则
     *
     * @param param
     * @return
     */
    @Override
    public ResultObject create(Map<String, Object> param) {
        ResultObject res = new ResultObject();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            if (null == param.get("columnsId") || null == param.get("columnsName")) {
                log.info("场景类型参数不全");
                res.setCode(StatusEnum.RECO_COLUMNS_ILLEGAL.getCode());
                res.setMsg(StatusEnum.RECO_COLUMNS_ILLEGAL.getMsg() + ": 场景类型参数不全");
                return res;
            }
            if (null == param.get("rescId") || null == param.get("rescName") || null == param.get("rescType") || null == param.get("offlineType")) {
                log.info("资源参数不全");
                res.setCode(StatusEnum.RECO_RESC_PARAMS_ILLEGAL.getCode());
                res.setMsg(StatusEnum.RECO_RESC_PARAMS_ILLEGAL.getMsg() + ": 指定资源参数不全");
                return res;
            }
            List<String> columnsId = Arrays.asList(String.valueOf(param.get("columnsId")).split(","));
            List<String> columnsName = Arrays.asList(String.valueOf(param.get("columnsName")).split(","));
            if (columnsId.size() == 0 || columnsName.size() == 0 || columnsId.size() != columnsName.size()) {
                log.info("场景类型参数不匹配，columnsId：{}个，columnsName：{}个", columnsId.size(), columnsName.size());
                res.setCode(StatusEnum.RECO_COLUMNS_ILLEGAL.getCode());
                res.setMsg(StatusEnum.RECO_COLUMNS_ILLEGAL.getMsg() + ": 场景类型参数不匹配");
                return res;
            }
            // 任务ID
            String taskId = String.valueOf(UUID.randomUUID());
            param.put("taskId", taskId);
            try {
                recoMapper.createResInfo(param);
            } catch (Exception e) {
                log.error("创建资源下线信息记录失败：{}", e.getMessage());
                res.setCode(StatusEnum.ERROR.getCode());
                res.setMsg(StatusEnum.ERROR.getMsg());
                return res;
            }
            // 遍历场景，并创建资源下线状态记录
            try {
                for (String columnId : columnsId) {
                    param.remove("columnsId");
                    param.put("columnsId", columnId);
                    recoMapper.createResStatus(param);
                }
            } catch (Exception e) {
                log.error("任务：{}创建资源下线状态记录失败：{}", taskId, e.getMessage());
                res.setCode(StatusEnum.ERROR.getCode());
                res.setMsg(StatusEnum.ERROR.getMsg());
                return res;
            }
            return res;
        } catch (Exception e) {
            log.error("创建出库规则失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 查询资源下线信息列表
     *
     * @param
     * @return
     */
    @Override
    public ResultObject queryRes(Map<String, Object> params) {
        ResultObject<Page<HashMap<String, Object>>> res = new ResultObject<>();
        try {
            if ("1".equals(params.get("isAdmin"))) {
                params.remove("creatorId");
                params.remove("creatorDepartment");
            } else {
                params.remove("cDepartment");
            }
            // 总数
            Integer total = recoMapper.resTotal(params);
            // 分页查询结果集
            List<HashMap<String, Object>> datas = recoMapper.resList(params);
            // 分页
            Page<HashMap<String, Object>> page = new Page<>();
            page.setTotal(total);
            page.setData(datas);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询资源下线信息列表失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 查询资源明细
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject query(Map<String, Object> params) {
        ResultObject res = new ResultObject();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            String resourceName = String.valueOf(params.get("rescName"));
            int resourceType = Integer.valueOf(String.valueOf(params.get("rescType")));
            res.setData(utils.inverse_search(resourceName, resourceType));
            return res;
        } catch (Exception e) {
            log.error("查询资源明细失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

}
